//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tuliu.appupdate.crash;

import android.text.TextUtils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;

class FileUtils {
    private static final int DEFAULT_BUFFER_SIZE = 8192;

    FileUtils() {
    }

    static String readFileToString(File file) {
        return readFileToString(file, (String)null);
    }

    static String readFileToString(File file, String encoding) {
        String result = null;
        if(file.exists()) {
            BufferedReader br = null;
            InputStreamReader isr = null;
            BufferedWriter bw = null;
            StringWriter sw = new StringWriter();

            try {
                isr = encoding == null?new InputStreamReader(new FileInputStream(file)):new InputStreamReader(new FileInputStream(file), encoding);
                br = new BufferedReader(isr);
                bw = new BufferedWriter(sw);
                char[] buffer = new char[8192];

                int e;
                while((e = br.read(buffer, 0, 8192)) != -1) {
                    bw.write(buffer, 0, e);
                }

                bw.flush();
                result = sw.toString();
            } catch (FileNotFoundException var19) {
                var19.printStackTrace();
            } catch (IOException var20) {
                var20.printStackTrace();
            } finally {
                try {
                    if(bw != null) {
                        bw.close();
                    }

                    if(br != null) {
                        br.close();
                    }

                    if(isr != null) {
                        isr.close();
                    }

                    sw.close();
                } catch (IOException var18) {
                    var18.printStackTrace();
                }

            }
        }

        return result;
    }

    static boolean writeBytesToFile(File file, byte[] bytes, boolean isAppend) {
        boolean isWriteOk = false;
        int count = 0;
        ByteArrayInputStream bais = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            if(!file.exists()) {
                createNewFileAndParentDir(file);
            }

            if(file.exists()) {
                bos = new BufferedOutputStream(new FileOutputStream(file, isAppend), 8192);
                bais = new ByteArrayInputStream(bytes);
                bis = new BufferedInputStream(bais, 8192);

                int e;
                for(byte[] buffer = new byte[8192]; (e = bis.read(buffer, 0, 8192)) != -1; count += e) {
                    bos.write(buffer, 0, e);
                }

                bos.flush();
            }

            isWriteOk = bytes.length == count;
        } catch (FileNotFoundException var20) {
            var20.printStackTrace();
        } catch (IOException var21) {
            var21.printStackTrace();
        } finally {
            try {
                if(bos != null) {
                    bos.close();
                }

                if(bis != null) {
                    bis.close();
                }

                if(bais != null) {
                    bais.close();
                }
            } catch (IOException var19) {
                var19.printStackTrace();
            }

        }

        return isWriteOk;
    }

    static boolean createNewFileAndParentDir(File file) {
        boolean isCreateNewFileOk = createParentDir(file);
        if(isCreateNewFileOk && !file.exists()) {
            try {
                isCreateNewFileOk = file.createNewFile();
            } catch (IOException var3) {
                var3.printStackTrace();
                isCreateNewFileOk = false;
            }
        }

        return isCreateNewFileOk;
    }

    static boolean createParentDir(File file) {
        boolean isMkdirs = true;
        if(!file.exists()) {
            File dir = file.getParentFile();
            if(!dir.exists()) {
                isMkdirs = dir.mkdirs();
            }
        }

        return isMkdirs;
    }

    static boolean deleteFileOrDirectory(String sPath) {
        File file = new File(sPath);
        return !file.exists()?false:(file.isFile()?deleteFile(sPath):deleteDirectory(sPath));
    }

    static boolean deleteFile(String sPath) {
        if(TextUtils.isEmpty(sPath)) {
            return false;
        } else {
            boolean flag = false;
            if(sPath.length() == 0) {
                return false;
            } else {
                File file = new File(sPath);
                if(file.isFile() && file.exists()) {
                    flag = file.delete();
                }

                return flag;
            }
        }
    }

    static boolean deleteDirectory(String sPath) {
        if(!sPath.endsWith(File.separator)) {
            sPath = sPath + File.separator;
        }

        File dirFile = new File(sPath);
        if(dirFile.exists() && dirFile.isDirectory()) {
            boolean flag = true;
            File[] files = dirFile.listFiles();
            if(files == null) {
                return false;
            } else {
                File[] arr$ = files;
                int len$ = files.length;

                for(int i$ = 0; i$ < len$; ++i$) {
                    File file = arr$[i$];
                    if(file.isFile()) {
                        flag = deleteFile(file.getAbsolutePath());
                        if(!flag) {
                            break;
                        }
                    } else {
                        flag = deleteDirectory(file.getAbsolutePath());
                        if(!flag) {
                            break;
                        }
                    }
                }

                return !flag?false:dirFile.delete();
            }
        } else {
            return false;
        }
    }
}
